package systemb;

import framework.*;
import framework.converters.ConvertFtMt;
import framework.converters.ConvertTempFC;
import framework.detectors.OutOfRangeDetector;
import framework.sinkers.FileSink;
import framework.sinkers.SystemsABCFormatter;

import java.io.FileNotFoundException;
import java.util.Hashtable;

/**
 */
public class SystemBPlumber {

	public static final int MIN_PRESSURE = 50;
    public static final int MAX_PRESSURE = 80;
    
    public static void main(String [] args) throws FileNotFoundException, InterruptedException {
    	
    	// verify if there are sufficient arguments to start the program
    	if(args.length != 3){
    		System.out.println("Wrong usage of program arguments!");
    		System.out.println("Systems usage: <Source filename> <Sink filename> <Sink WP_Pressure filename>\n \t ");
    		System.exit(0);
    	}
    	
    	String argSource		= args[0];
        String argSink			= args[1];
        String argSinkWPPress	= args[2];
    	
    	// create the data source
        FileDataSource src = new FileDataSource(argSource);

        //create the main data sink
        int [] printOrder = {Constants.TIME_ID,Constants.TEMPERATURE_ID,Constants.ALTITUDE_ID,Constants.PRESSURE_ID};
        FileSink sink = new FileSink(argSink, printOrder, new SystemsABCFormatter());

        //create the wp data sink
        int [] printOrderWP = {Constants.TIME_ID,Constants.PRESSURE_ID};
        FileSink wpSink = new FileSink(argSinkWPPress, printOrderWP, new SystemsABCFormatter());

        //create the converter
        Hashtable<Integer, IConvertFunction> mappings = new Hashtable<Integer, IConvertFunction>();
        mappings.put(Constants.TEMPERATURE_ID, new ConvertTempFC());
        mappings.put(Constants.ALTITUDE_ID, new ConvertFtMt());
        Converter conv = new Converter(mappings);

        //create the dropper
        int [] toDrop = {Constants.VELOCITY_ID,Constants.ATTITUDE_ID};
        Dropper drop = new Dropper(toDrop);

        //create the WP extrapolator
        int [] idsToExtrapolate = {Constants.PRESSURE_ID};
        OutOfRangeDetector pressOutRangeDetector = new OutOfRangeDetector(MIN_PRESSURE,MAX_PRESSURE);
        WildPointExtrapolator wpExtrapol = new WildPointExtrapolator(idsToExtrapolate,pressOutRangeDetector);

        //connect everything
        sink.Connect(wpExtrapol,0);
        wpSink.Connect(wpExtrapol,1);
        wpExtrapol.Connect(conv,0,0);
        conv.Connect(drop,0);
        drop.Connect(src,0);

        //start filters
        src.start();
        drop.start();
        conv.start();
        wpExtrapol.start();
        wpSink.start();
        sink.start();
    }

}
